package com.njwb.provider.controller;

import com.njwb.common.json.ResponseResult;
import com.njwb.common.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "用户访问接口", description = "用户服务相关的API接口")
@RestController
public class UserController {
    //模拟user表数据
    private static Map<Integer, User> users = new HashMap<Integer, User>();
    private static Integer id = 1;
    @Operation(description = "添加用户")
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @PostMapping("user")
    public ResponseResult add(User user){
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }
    @Operation(description = "根据Id删除用户")
    @Parameter(name = "id",description = "用户Id")
    @DeleteMapping("user/{id}")
    public ResponseResult delById(@PathVariable("id") Integer id){
        users.remove(id);
        return ResponseResult.success();
    }

    @Operation(description = "修改用户")
    @Parameters({
            @Parameter(name = "id",description = "用户名Id"),
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @PutMapping("user")
    public ResponseResult modify(User user){
        System.out.println(user);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @Operation(description = "根据Id查询用户信息")
    @Parameter(name = "id",description = "用户Id")
    @GetMapping("user/{id}")
    public ResponseResult getById(@PathVariable("id") Integer id){
        User user=users.get(id);
        return ResponseResult.success(user);
    }

    @Operation(description = "查询所有用户信息")
    @GetMapping("user")
    public ResponseResult findAll(){
        List<User> list=new ArrayList<User>(users.values());
        return ResponseResult.success(list);
    }
}
